<template>
  <div style="margin-top:25px;">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="150px">
      <el-form-item prop="headUrl" label="头像">
        <el-upload
          class="avatar-uploader"
          :action="action"
          :show-file-list="false"
          :on-success="xczUpload">
          <img v-if="dataForm.headUrl" :src="dataForm.headUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
       <el-form-item prop="username" label="用户名称">
        <el-input v-model="dataForm.username"></el-input>
      </el-form-item>
      <el-form-item prop="gender" label="性别" size="mini">
        <el-radio-group v-model="dataForm.gender">
          <el-radio :label="0">男</el-radio>
          <el-radio :label="1">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="introduce" label="个人介绍">
        <el-input type="textarea" :rows="3" v-model="dataForm.introduce"></el-input>
      </el-form-item>
    </el-form>
    <div style="text-align:center;margin-top:25px;">
      <el-button type="primary" @click="dataFormSubmitHandle()">保存</el-button>
    </div>
  </div>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        gender: 0
      },
      userId: '',
      action: window.SITE_CONFIG['apiURL'] + '/sys/oss/upload'
    }
  },
  computed: {
    dataRule () {
      return {
        headUrl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        username: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.userId = this.$store.state.user.id
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getInfo()
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/xnkt/tbexpert/getExpertInfoByUserId/${this.userId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data,
          userId: this.userId
        }
      }).catch(() => {})
    },
    // 上传头像
    xczUpload (file) {
      if (file.code !== 0) {
        this.$message.error(file.msg)
        return false
      }
      this.$set(this.dataForm, 'headUrl', file.data.src)
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.post('/sys/user/updateUserSimpleInfo', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
